/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.kota5.palette;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import javax.swing.JComponent;
import org.netbeans.api.visual.action.AcceptProvider;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.ConnectorState;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;

/**
 *
 * @author cup
 */
public class GraphSceneImpl extends GraphScene<MyNode, String>{
    private LayerWidget mainLayer;

    public GraphSceneImpl() {
        mainLayer = new LayerWidget (this);
        addChild (mainLayer);
        getActions().addAction(ActionFactory.createAcceptAction(new AcceptProvider() {

            @Override
            public ConnectorState isAcceptable(Widget widget, Point point, Transferable t) {
                Image dragImage = getImageFromTransferable(t);
                JComponent view = getView();
                Graphics2D g2 = (Graphics2D) view.getGraphics();
                Rectangle visRect = view.getVisibleRect();
                view.paintImmediately(visRect.x, visRect.y, visRect.width, visRect.height);
                g2.drawImage(dragImage,AffineTransform.getTranslateInstance(point.getLocation().getX(),point.getLocation().getY()), null);
                return ConnectorState.ACCEPT;
            }

            @Override
            public void accept(Widget widget, Point point, Transferable t) {
                Image image = getImageFromTransferable(t);
                Widget w = GraphSceneImpl.this.addNode(new MyNode(image));
                w.setPreferredLocation(widget.convertLocalToScene(point));
            }

            private Image getImageFromTransferable(Transferable t) {
                Object o = null;
                try{
                    o = t.getTransferData(DataFlavor.imageFlavor);
                }catch(IOException ex){
                    ex.printStackTrace();
                }catch(UnsupportedFlavorException ex){
                    ex.printStackTrace();
                }
                return o instanceof Image ? (Image) o : org.openide.util.Utilities.loadImage("org/kota5/palette/widget/spinner_normal.9.png");
            }
        }));
        getActions().addAction(ActionFactory.createZoomAction());
    }

 
    @Override
    protected Widget attachNodeWidget(MyNode n) {
        IconNodeWidget widget = new IconNodeWidget(this);
        widget.setImage(n.getImage());
        widget.getActions().addAction(ActionFactory.createMoveAction());
        mainLayer.addChild(widget);
        return widget;
    }

    @Override
    protected Widget attachEdgeWidget(String e) {
        return null;
    }

    @Override
    protected void attachEdgeSourceAnchor(String e, MyNode n, MyNode n1) {

    }

    @Override
    protected void attachEdgeTargetAnchor(String e, MyNode n, MyNode n1) {

    }

}
